package com.lmf.serviceUcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmf.handler.Exception.LmfException;
import com.lmf.resultCode.JwtUtils;
import com.lmf.resultCode.MD5;
import com.lmf.serviceUcenter.entity.UcenterMember;
import com.lmf.serviceUcenter.entity.vo.RegisterVo;
import com.lmf.serviceUcenter.entity.vo.UcenterQuery;
import com.lmf.serviceUcenter.mapper.UcenterMemberMapper;
import com.lmf.serviceUcenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lmf
 * @since 2022-02-09
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //用户登录验证密码账号
    @Override
    public String loginAccount(UcenterQuery ucenterQuery) throws LmfException {
        String phone = ucenterQuery.getPhone();
        String password = ucenterQuery.getPassword();
        //1.判断账号密码是否为空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
            throw new LmfException(20001,"账号或者密码为空！");
        }
        //2.根据手机号去数据库里面查询，是否存在该账号密码对象
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        UcenterMember ucenterMember = this.getOne(wrapper);

        //判断该对象是否为空
        if (StringUtils.isEmpty(ucenterMember)) {
            throw new LmfException(20001,"该账号尚未注册！请先注册再登录");
        }

        //判断该账号的密码是否正确
        //要先给前端传回来的password加密，再去与数据库存储的密码对比
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new LmfException(20001,"账户的密码不正确");
        }

        //判断用户是否禁用
        if (ucenterMember.getIsDisabled()){
            throw new LmfException(20001,"用户被禁用");
        }

        //3.登录成功，返回一个身份令牌
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }

    //注册用户
    @Override
    public void registerAccount(RegisterVo registerVo) throws LmfException {
        //验证码
        String code = registerVo.getCode();
        //手机号
        String mobile = registerVo.getMobile();
        //昵称
        String nickname = registerVo.getNickname();
        //密码
        String password = registerVo.getPassword();

        //判断非空
        if (password == null || nickname == null || mobile == null || code == null) {
            throw new LmfException(20002,"有必要数据为空，注册失败！");
        }

        //判断验证码是否正确以及过时
        //从redis中获取验证码
        String s = redisTemplate.opsForValue().get(mobile);
        //再与前端传回来的验证码比对，是否正确
        if (!s.equals(code)) {
            throw new LmfException(20002,"验证码不正确，注册失败！");
        }

        //判断账号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        int count = this.count(wrapper);
        if (count >= 1) {
            throw new LmfException(20001,"账号(手机号)重复，注册失败");
        }

        //将相关数据保存到数据库中，注册成功
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("http://edu-guli-lmf.oss-cn-beijing.aliyuncs.com/2022/01/29/96eb8d2e546440d29f951b9dd17e12211635491881952.jpeg");
        boolean save = this.save(ucenterMember);
        if (!save) {
            throw new LmfException(20001,"保存数据失败，注册失败");
        }

    }
}
